package de.bazinga.rotw.weapon.pistol.bullet;

import com.jme.bounding.BoundingSphere;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.CullState;
import com.jme.scene.state.MaterialState;

import de.bazinga.rotw.game.Game;
import de.bazinga.rotw.person.Person;
import de.bazinga.rotw.texture.TextureCache;
import de.bazinga.rotw.util.GlobalSettings;

public class Bullet extends Sphere {
	private static final long serialVersionUID = 1L;
	
	private ColorRGBA bulletColor = null;
	
	private Vector3f startPosition = null;

	private Person player = null;
	
	private BulletController controller = null;
	
	private boolean active = false;

	//private TrailMesh trailMesh;
	public Bullet(Node rootNode, Person player, Vector3f location, Vector3f direction, ColorRGBA color/*,boolean withController*/) {
		super("bullet",new Vector3f(0,0,0),10,10,0.05f);
		this.setLocalTranslation(location);
		
		this.startPosition = location.clone();
		this.player = player;
		
		this.bulletColor = color;
		this.active = true;
		
		
		setRenderState(color);
		
		this.updateGeometricState(0.0f, false);

		rootNode.attachChild(this);
		rootNode.updateRenderState();
		
		
		
		
		
		// Add Controller
		controller = new BulletController(this,direction,GlobalSettings.BULLETSPEED);
		this.addController(controller);		
	}
	
	
	public void reset(Person player, Vector3f location, Vector3f direction, ColorRGBA color) {
		this.setLocalTranslation(location);
		this.startPosition = location.clone();
		this.player = player;
		this.bulletColor = color;
		
		setRenderState(color);

		this.updateGeometricState(0.0f, false);
		
		if(controller != null) {
			controller.setDirection(direction);
		}
	}
	
	
	public void setRenderState(ColorRGBA color) {
		MaterialState ms = Game.getGame().getDisplay().getRenderer().createMaterialState();
		ms.setAmbient(color);
		ms.setDiffuse(color);
		ms.setEmissive(color);
		this.setRenderState(ms);
		
		this.setModelBound(new BoundingSphere());
		this.updateModelBound();
		
		this.setDefaultColor(color);
		
		this.setRenderState(TextureCache.getInstance().getTexture("texture/rust.jpg"));
		
		CullState cull = Game.getGame().getDisplay().getRenderer().createCullState();
		cull.setCullFace(CullState.Face.Back);
		this.setRenderState(cull);
		
		this.updateRenderState();
	}
	
	public ColorRGBA getBulletColor() {
		return bulletColor;
	}
	
	public Vector3f getStartPosition() {
		return startPosition;
	}
	
	public Person getPlayer() {
		return player;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		if(active) {
			// Set Active
			controller.calculateMaxDistance();
			controller.setActive(true);
			this.setCullHint(CullHint.Never);
		}else{
			// Set Inactive
			
			// Remove from rootnode?
			
			controller.setActive(false);
			this.setLocalTranslation(new Vector3f(9999,9999,9999));
			this.setCullHint(CullHint.Always);
			BulletList.getInstance().addUnusedBullet(this);
			BulletList.getInstance().removeBullet(this); // Remove Bullet
		}
		
		this.active = active;
	}
	
	public void setDamage(int top, int middle, int bottom) {
		if(controller != null) {
			controller.setDamage(top, middle, bottom);
		}
	}
}
